package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Shoppingcart;
import com.fasteat.mall.service.ShoppingcartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ghost
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/mall/shoppingcart")
@Api(description = "购物车管理模块")
@CrossOrigin
public class ShoppingcartController {

	@Autowired
	private ShoppingcartService shoppingcartService;

	@PostMapping
	@ApiOperation("将商品加入到购物车中")
	public Result addProductInShoppingcart(@RequestBody @ApiParam(value = "购物车订单对象") Shoppingcart shoppingcart) {
		if (shoppingcart == null
				|| StringUtils.isEmpty(shoppingcart.getProductId())
				|| StringUtils.isEmpty(shoppingcart.getUserId())
				|| shoppingcart.getAmount() <= 0
				|| StringUtils.isEmpty(shoppingcart.getProductTitle())) {
			return Result.error("添加失败");
		}
		// 计算总金额和总数量
		QueryWrapper<Shoppingcart> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", shoppingcart.getUserId());
		wrapper.eq("product_id", shoppingcart.getProductId());
		int count = shoppingcartService.count(wrapper);
		if (count == 0) {
			shoppingcart.setTotalMoney(shoppingcart.getSalePrice().multiply(new BigDecimal(shoppingcart.getAmount())));
			boolean ret = shoppingcartService.save(shoppingcart);
			return ret ? Result.success("添加成功") : Result.error("添加失败");
		}
		Shoppingcart shoppingcart2 = shoppingcartService.getOne(wrapper);
		shoppingcartService.removeById(shoppingcart2.getId());
		shoppingcart.setAmount(shoppingcart.getAmount() + shoppingcart2.getAmount());
		shoppingcart.setTotalMoney(shoppingcart.getSalePrice().multiply(new BigDecimal(shoppingcart.getAmount())));
		boolean ret = shoppingcartService.save(shoppingcart);
		return ret ? Result.success("添加成功") : Result.error("添加失败");
	}

	@GetMapping("/{userId}/{size}/{current}")
	@ApiOperation("查看购物车中的订单（带分页）")
	public Result getShoppingcartOrder(@PathVariable(name = "userId") @ApiParam(required = true, value = "用户ID") String userId,
									   @PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
									   @PathVariable(name = "current") @ApiParam(required = true, value = "页码") int current) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查看失败");
		}
		size = size <= 0 ? 5 : size;
		current = current <= 0 ? 1 : current;
		Page<Shoppingcart> page = new Page<>(current, size);
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("user_id", userId);
		shoppingcartService.page(page, wrapper);
		return Result.success().data(page);
	}


	@DeleteMapping("/{id}")
	@ApiOperation("删除购物车中的订单")
	public Result deleteShoppingcartOrder(@PathVariable(name = "id") @ApiParam(required = true, value = "id") String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		boolean ret = shoppingcartService.removeById(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	@PutMapping
	@ApiOperation("修改购物车中的订单内容")
	public Result updateShoppingcartOrder(@RequestBody @ApiParam(value = "购物车订单对象") Shoppingcart shoppingcart) {
		if (shoppingcart == null
				|| shoppingcart.getAmount() < 0
				|| StringUtils.isEmpty(shoppingcart.getId())) {
			return Result.error("修改失败");
		}
		boolean ret = shoppingcartService.updateById(shoppingcart);
		return ret ? Result.success("修改成功") : Result.error("修改失败");
	}

}

